﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Models;
using FreedomERP.Comm;
using System.Collections;
using System.Data.Objects;
using System.Text;
using FreedomERP.Comm.Utils;

namespace FreedomERP.Dao.Sd.Impl
{
    public class SdSaleOrderDAOImpl : ISdSaleOrderDAO
    {
        FreedomERPEntities dbEntities = new FreedomERPEntities();

        /// <summary>
        /// 根据条件获取列表
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public List<SdSaleOrder> QuerySaleOrderPaggingByParam(PagingParams paggingParams, out int totalCount) 
        {
            Hashtable filters = paggingParams.filters;

            //where
            List<ObjectParameter> paramList = new List<ObjectParameter>();
            StringBuilder sbWhere = new StringBuilder();
            if (filters["FNumberParam"] != null && !filters["FNumberParam"].ToString().Equals(""))
            {
                sbWhere.Append("it.FNumber like @FNumberParam and ");
                ObjectParameter param = new ObjectParameter("FNumberParam", "%" + filters["FNumberParam"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["FBillDateFrom"] != null && !filters["FBillDateFrom"].ToString().Equals(""))
            {
                sbWhere.Append("it.FBillDate between @FBillDateFrom and @FBillDateTo and ");
                ObjectParameter param = new ObjectParameter("FBillDateFrom", DateUtils.ParseStringToDateTime(filters["FBillDateFrom"].ToString()));
                paramList.Add(param);
                string FBillDateTo = "";
                if (filters["FBillDateTo"] != null && !filters["FBillDateTo"].ToString().Equals(""))
                {
                    FBillDateTo = filters["FBillDateTo"].ToString();
                }
                else
                {
                    FBillDateTo = DateUtils.GetCurDateFormat("yyyy/MM/dd HH:mm:ss");
                }
                ObjectParameter paramT = new ObjectParameter("FBillDateTo", DateUtils.ParseStringToDateTime(FBillDateTo));
                paramList.Add(paramT);
            }
            if (filters["FStatusId"] != null && !filters["FStatusId"].ToString().Equals(""))
            {
                sbWhere.Append("it.FStatusId=@FStatusId and ");
                ObjectParameter param = new ObjectParameter("FStatusId", filters["FStatusId"].ToString());
                paramList.Add(param);
            }
            if (filters["FBusinessCategoryId"] != null && !filters["FBusinessCategoryId"].ToString().Equals(""))
            {
                sbWhere.Append("it.FBusinessCategoryId=@FBusinessCategoryId and ");
                ObjectParameter param = new ObjectParameter("FBusinessCategoryId", filters["FBusinessCategoryId"].ToString());
                paramList.Add(param);
            }
            if (filters["FBarcode"] != null && !filters["FBarcode"].ToString().Equals(""))
            {
                string FBarcode = filters["FBarcode"].ToString();
                var querySub = (from sod in dbEntities.SdSaleOrderDetail where sod.FBarcode == FBarcode select sod.FParentId).ToList<string>();
                if (querySub != null && querySub.Count > 0)
                {
                    StringBuilder sbSub = new StringBuilder();
                    foreach (string FParentId in querySub)
                    {
                        sbSub.Append("'" + FParentId + "',");
                    }
                    sbSub.Length = sbSub.Length - 1;
                    sbWhere.Append("it.FId in {" + sbSub.ToString() + "} and ");
                }
                else
                {
                    sbWhere.Append("1!=1 and ");
                }
                
            }

            sbWhere.Append("it.FOrgunitId=@orgunitParam and ");
            ObjectParameter paramOrg = new ObjectParameter("orgunitParam", filters["orgunitParam"].ToString());
            paramList.Add(paramOrg);

            sbWhere.Append("1=1");

            //order by
            StringBuilder sbOrder = new StringBuilder();
            sbOrder.Append("it." + paggingParams.Sort + " " + paggingParams.Dir);

            ObjectParameter[] objParameters = new ObjectParameter[paramList.Count];
            int c = 0;
            foreach (ObjectParameter paramTemp in paramList)
            {
                objParameters[c] = paramTemp;
                c++;
            }

            var query = dbEntities.SdSaleOrder.Where(sbWhere.ToString(), objParameters).OrderBy(sbOrder.ToString());
            totalCount = query.Count();

            List<SdSaleOrder> list = query.Skip(paggingParams.Start).Take(paggingParams.Limit).ToList();

            return list;
        }

        /// <summary>
        /// 根据FId获取订单信息
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public SdSaleOrder GetSaleOrderByFId(String FId)
        {
            SdSaleOrder saleOrder = dbEntities.SdSaleOrder.FirstOrDefault(ro => ro.FId == FId);
            return saleOrder;
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void AddSaleOrder(SdSaleOrder saleOrder)
        {
            dbEntities.SdSaleOrder.AddObject(saleOrder);
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public void Save()
        {
            dbEntities.SaveChanges();
        }
    }
}